package com.gency.subscribe.web.controller.system;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.plugins.Page;
import com.gency.subscribe.model.system.PageBean;
import com.gency.subscribe.model.system.ProName;
import com.gency.subscribe.service.system.IProNameService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马行空
 * @since 2018-06-27
 */
@Controller
@RequestMapping("/proName")
public class ProNameController {

	@Autowired
	IProNameService proNameService;
	
	@GetMapping("/list")
	public ModelAndView list(PageBean pb,ModelAndView mv){
		Page<ProName> page = new Page<ProName>(pb.getCurPage(),pb.getPageSize());
		page.setAsc(false);
		page.setOrderByField("create_time");
		page = proNameService.selectPage(page);
		List<ProName> list = page.getRecords();
		pb.setAllRow(page.getTotal());
		pb.setCurPage(page.getCurrent());
		pb.setTotalPage(page.getPages());
		pb.setNextPage(page.getOffset());
		pb.setBeforPage(page.getOffsetCurrent());
		pb.setPageSize(10);
		pb.setStartRow(1);
		mv.addObject("pagebean",pb);
		mv.addObject("proNamelist", list);
		mv.setViewName("porname/list");
		return mv;
	}
	
	@GetMapping("/add")
	public ModelAndView add(ModelAndView mv){
		mv.addObject("proName", new ProName());
		mv.setViewName("porname/info");
		return mv;
	}
	
	@PostMapping("/add")
	public ModelAndView add(ModelAndView mv,@Valid ProName departType,BindingResult result){
		proNameService.insert(departType);
		mv.setViewName("redirect:list");
		return mv;
	}
	
	@GetMapping("/update")
	public ModelAndView update(ModelAndView mv,Long id){
		mv.setViewName("porname/info");
		mv.addObject("proName",proNameService.selectById(id));
		return mv;
	}
	
	@PostMapping("/update")
	public ModelAndView update(ModelAndView mv,@Valid ProName departType,BindingResult result){
		proNameService.updateById(departType);
		mv.setViewName("redirect:list");
		return mv;
	}
	
	@GetMapping("/delete")
	public ModelAndView delete(ModelAndView mv,Long id){
		mv.setViewName("redirect:list");
		proNameService.deleteById(id);
		return mv;
	}
	
	
}

